package gpt

import (
	"encoding/json"
	"fmt"
	"gpt/pojo"
	"io/ioutil"
	"net/http"
	"strings"
)

func ChatModel(questtion string) pojo.Response {
	// 创建一个新的HTTP客户端
	client := &http.Client{}

	reqBody := pojo.Request{
		Model: "qwen-turbo",
		Print: pojo.Input{
			Prompt: questtion,
		},
	}

	b, err2 := json.Marshal(reqBody)
	//fmt.Println(string(b))
	if err2 != nil {
		fmt.Println(err2)
		return pojo.Response{}
	}

	r := strings.NewReader(string(b))

	// 创建一个GET请求
	req, err := http.NewRequest("POST", "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation", r)
	if err != nil {
		fmt.Println("Error creating request:", err)
		return pojo.Response{}
	}
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "test-sk-8495b988f06046d08c11df126beadb52-demo")

	// 发送请求并获取响应
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error sending request:", err)
		return pojo.Response{}
	}
	defer resp.Body.Close()

	// 读取响应体
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response:", err)
		return pojo.Response{}
	}

	// 打印响应内容
	//fmt.Println("Response:", string(body))
	var res pojo.Response
	err3 := json.Unmarshal(body, &res)
	if err3 != nil {
		fmt.Println(err3)
		return pojo.Response{}
	}
	//fmt.Println(res.Output.Text)
	return res
}
